package com.application.magazine.bookitem;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.magazine.Bookmarks;
import com.application.magazine.MagazineBookRead;
import com.application.magazine.R;



public class BookmarksAdapter extends BaseAdapter {
	
	private Context context;
	private final List<Bookmarks> bookmarks;
	public Activity magazineBookSportMainActivity;
	public BookmarksAdapter(Context context, List<Bookmarks> bookmarks, Activity a) {
		this.context = context;
		this.bookmarks = bookmarks;
		this.magazineBookSportMainActivity = a;
	} 
	
	@Override
	public int getCount() {
		return bookmarks.size();
	}

	@Override
	public View getView(final int position, final View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 
		View gridView;
	 
		if (convertView == null) {
			
			gridView = new View(context);
	 
			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.book_item_layout, null);
	  
			final Bookmarks bookmark = bookmarks.get(position);
			boolean status = false;
			if (bookmark != null) {
				TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
				textView.setText(bookmark.magazine.title);

				try {
					String path = bookmark.path;
					File imgFile = new File(path);
					if(imgFile.isFile()){
					Bitmap icon = BitmapFactory.decodeFile(path);
					
					ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_cover);
					imageView.setImageBitmap(icon);
					}

				}catch (Exception e) {
					// TODO: handle exception
				} 
				ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_new);
				imageView.setVisibility(status ? View.VISIBLE : View.INVISIBLE);
				
				
				//Manage clickable book
				FrameLayout bookclick = (FrameLayout) gridView.findViewById(R.id.BookClick);
				bookclick.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(magazineBookSportMainActivity, MagazineBookRead.class);
						SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
						  SharedPreferences.Editor editor = preferences.edit();
						  editor.putInt("mag_id",bookmark.magazine.id);
						  editor.putBoolean("bookmark",true);
						  editor.putInt("page", new Integer(bookmark.page));
						  editor.commit();
						magazineBookSportMainActivity.startActivity(i);
						magazineBookSportMainActivity.finish();
					}	
				});
				
				
			} else {
				//TODO error
			}
	 	 
		} else {
			gridView = (View) convertView;
		}
		
		return gridView;
	}
	

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
}
